﻿

#导入sys pygame random 库 以及pygame.locals内的所有子程序
import sys,pygame ,random
from pygame.locals import *


#初始化
pygame.init()

#定义框架的尺寸
width,height = 450,450

#定义窗口大小

screen = pygame.display.set_mode((width,height))

#调整颜色

black,red,blue,white = (0,0,0),(255,0,0),(0,0,255),(255,255,255)


#创建落子状态
xxx,empty,ooo = -1,0,1
board = [[empty]*3,[empty]*3,[empty]*3]
#不知道
turn = xxx
#创建背景的函数



            
    


#让程序刷新
 
    


#判断胜利
def victory():
    global board, running
    for val in range(3):
        if board[0][val] == board[1][val] == board[2][val] != empty:
            running = 0
            pygame.draw.line(screen, blue, (val*160+80, 0), (val*160+80, 480), 2)
        if board[val][0] == board[val][1] == board[val][2] != empty:
            running = 0
            pygame.draw.line(screen, blue, (0, val*160+80), (480, val*160+80), 2)
    if board[0][0] == board[1][1] == board[2][2] != empty:
        running = 0
        pygame.draw.line(screen, blue, (0, 0), (480, 480), 2)
    if board[0][2] == board[1][1] == board[2][0] != empty:
        running = 0
        pygame.draw.line(screen, blue, (0, 480), (480, 0), 2)
    if all(val != empty for line in board for val in line):
        running = 0
    
running = 1
while running:
    pos = None
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        if event.type == MOUSEBUTTONDOWN and event.button == 1:
            pos = (event.pos[1]//160, event.pos[0]//160)


#下棋

    if pos!=None:
        row, col = pos
        if board[row][col] == empty:
            board[row][col] = turn
            turn *= -1

     #绘制一个矩形（位置，bgc，（左边框，上边框，宽，高），填充矩形内部）
    screen.fill(white)
#绘制直线
    pygame.draw.line(screen,black,(150,0),(150,450),5)
    pygame.draw.line(screen,black,(300,0),(300,450),5)
    pygame.draw.line(screen,black,(0,150),(450,150),5)
    pygame.draw.line(screen,black,(0,300),(450,300),5)



##确定棋盘位置
    for row, line in enumerate(board):
        for col, val in enumerate(line):
            if val == xxx:
                # Draw an X as two crossing lines.
                #绘制x

                
                upper_left = (col * 150 + 5, row * 150 + 5)
                lower_right = (col * 150 + 155, row * 150 + 155)
                pygame.draw.line(screen, red, upper_left, lower_right, 5)

                upper_right = (col * 150 + 155, row * 150 + 5)
                lower_left = (col * 150 + 5, row * 150 + 155)
                pygame.draw.line(screen, red, upper_right, lower_left, 5)
            elif val == ooo:
                #绘制o

                
                rect = (col * 150 + 5, row * 150 + 5, 150, 150)
                pygame.draw.ellipse(screen, blue, rect, 5)
            else:
                assert val == empty
                continue

    victory()
#让程序刷新    
    pygame.display.flip()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
    
#刷新

#创建主程序

#让命令等待
    

    


        
 #调用画背景函数
 
